package TTEST;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import Files.PLIKI;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.dyno.visual.swing.layouts.Bilateral;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;
import org.dyno.visual.swing.layouts.Trailing;

import a.jframe.Lab;
import Files.File_;
import ch.ethz.ssh2.Session;

//VS4E -- DO NOT REMOVE THIS LINE!
public class LabWindow extends JInternalFrame {
	/**
	 * class parsing commands which has executed
	 * 
	 * @author hyskimat
	 * 
	 * 
	 * 
	 */
	class ParseCommand {
		List<String> rr = new ArrayList<String>();

		void Upload() {
			System.out.println("w U");
		}

		void Provision() {
			System.out.println("w P");
		}

		public ParseCommand(String aa) {

			for (String h : aa.split(" ")) {
				if (h.contains("racclimx.sh"))
					if (h.contains(RACCLIMX) || h.contains("Upload"))
						Upload();
					else if (h.contains("Provision"))
						Provision();
					else //own commands

				if (h.contains("=true") || h.contains("=false"))
					System.out.println("TF");
			}
		}
	}
	 static boolean koniecpolecenia=false;
	// END CLASS
	public static boolean isPolaczenie_z_ip() {
		return polaczenie_z_ip;
	}

	public static void setPolaczenie_z_ip(boolean polaczenie_z_ip) {
		LabWindow.polaczenie_z_ip = polaczenie_z_ip;
	}

	public LabWindow(String a, String b) {
		this.host = a;
		this.username = b;
	}
	public LabWindow(String a, String b,String c) {
		this.host = a;
		this.username = b;
		this.shortName=c;
	}
	public boolean offline=false;

	private void checkofflineflag(String[] r)
	{
		for(int a=0;a<r.length;a++)
		{
			if ( r[a].indexOf("&false")!=-1)
			{
				offline=false;
				System.out.println("false");
			}
			if ( r[a].indexOf("&true")!=-1)
			{
				offline=true;
				System.out.println("true");
			}
		}
	}
	public LabWindow(String h) {
		String [] w= h.split(" ");
		h=w[0];
		String[] r = File_.common_map_labs.get(h).split(" ");
		checkofflineflag(w);
		System.out.println("common>"+r[0]+r[1]+r[2]);
		shortName=h;
		setClosable(true);
		setMaximizable(true);
		String rightvalue = h;
		setTitle(rightvalue);
		DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd HH_mm_ss");
		Calendar cal = Calendar.getInstance();
		directory = new File_().getbasedirectory();
		
		
		if( !offline )
		{
		if (!new File(directory).mkdir())
			new File(directory).mkdir();
		polaczenie_z_ip = false;
		do {
			try {
				Thread.sleep(500);
				System.out.println("Czekam...");
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
			;
		} while (polaczenie_z_ip);
		ct = new _ConnectionThread(this, getJTextArea0(), host = r[2], r[0],
				r[1]);
		ct.start();
		}
		try {
			initComponents();
		} catch (exc.twoDefaultValuesCanNotExists t) {

		}
		try {
			initComponents();
		} catch (exc.twoDefaultValuesCanNotExists t) {

		}
	}

	public LabWindow() {
		try {
			initComponents();
		} catch (exc.twoDefaultValuesCanNotExists t) {

		}
	}

	private void initComponents() throws exc.twoDefaultValuesCanNotExists {
		setVisible(true);
		setLayout(new GroupLayout());
		add(getJTabbedPane0(), new Constraints(new Leading(13, 1528, 12, 12),
				new Bilateral(560, 12, 7)));
		add(getJLabel1(), new Constraints(new Leading(11, 1519, 10, 10),
				new Leading(530 + 10, 169, 169)));
		add(getJScrollPane1(), new Constraints(new Leading(11, 1522, 10, 10),
				new Leading(8, 510, 10, 10)));
		add(createComboBoxWithCommandsHistory(), new Constraints(new Leading(
				11, 1519, 10, 10), new Leading(517, 169, 169)));
		add(getJScrollPane0(), new Constraints(new Leading(1275, 250, 10, 10),
				new Trailing(96, 135, 822, 822)));
		setSize(1560, 849);
		/**
		 * only during first show
		 */
		putcommandintogui(mapWithStandardCommands.get(actualselectedTab));
	}

	/**
	 * execute command selected from list
	 * 
	 * @return
	 */
	private JButton runCommandSelectedFromList() {
		if (jButton3 == null) {
			jButton3 = new JButton();
			jButton3.setText("Execute");
			changeVisibilityButtonResponsibleToExecuteCommandfroomList(false);
			jButton3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					addFlag = true;
					runCommandFromLabel(event);
					//resetpanel("Provision");
					changeVisibilityButtonResponsibleToExecuteCommandfroomList(false);
					addFlag = false;
				}
			});
		}
		return jButton3;
	}
	private JButton openEditableFormForCommands() {
		if (jButton4 == null) {
			jButton4 = new JButton();
			jButton4.setText("Open My Commands");
			changeVisibilityButtonResponsibleToExecuteCommandfroomList(false);
			jButton4.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					addFlag = true;
					//open form
					InternalFrameDemo.open();
					//runCommandFromLabel(event);
					changeVisibilityButtonResponsibleToExecuteCommandfroomList(false);
					addFlag = false;
				}
			});
		}
		return jButton4;
	}

	private static void changeVisibilityButtonResponsibleToExecuteCommandfroomList(
			boolean flag) {
		jButton3.setEnabled(flag);
	}

	void changeVisibilityButtonresponsibleToSaveInsertedCommand(boolean flag) {
		if (!flag) {
			jTextField1.setText("");
			jTextField1.setEnabled(false);
			jButton2.setEnabled(false);

		} else
		{
			jTextField1.setEnabled(true);
		}
	}

	private JScrollPane ListWithCommandsFromFile() {
		if (jScrollPane3 == null) {
			jScrollPane3 = new JScrollPane();
			jScrollPane3.setName("Lista");
			jScrollPane3.setViewportView(listWithCommandsFromFile());
		}
		return jScrollPane3;
	}

	/**
	 * list with commands from file
	 * 
	 * @return
	 */
	private JList listWithCommandsFromFile() {
		if (jList2 == null) {
			jList2 = new JList();
			jList2.setName("listWithMyCommands");
			jList2.setSize(300, 350);
			listModel = new DefaultListModel();
			if (File_.common_map_commands != null)
				fillListModel(File_.common_map_commands);
			else
				jList2.setEnabled(false);
			jList2.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent event) {
					executeCommandFromList(event);
				}
			});
		}
		return jList2;
	}

	private void fillListModel(Map<String, String> c) {
		listModel.clear();
		if (!c.isEmpty()) {
			for (String value : c.keySet()) {
				listModel.addElement(value);
			}
			jList2.setModel(listModel);
		}
	}

	private JButton saveInsertedCommandIntoConsoleIntoFile() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setText("Save");
			jButton2.setEnabled(false);
			jButton2.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					if (new File_().addToMapLab(jTextField1.getText(),
							commandPutFromTextArea, 0)) {
						if (File_.common_map_commands != null) {
							fillListModel(File_.common_map_commands);
						} else
							jList2.setEnabled(false);
					}
				}
			});
		}
		return jButton2;
	}

	private JPanel generatePanelMYCOMMANDS() {
		if (jPanel6 == null) {
			jPanel6 = new JPanel();
			jPanel6.setLayout(new GroupLayout());
			jPanel6.add(getJTextField1(), new Constraints(new Leading(528 /*+ 15*/,
					295 /*+ 15*//* 279 */, 12, 12), new Leading(195 /*+ 15*/, 10, 10)));
			jPanel6.add(getJLabel2(), new Constraints(new Leading(237, 12, 12),
					new Leading(197, 12, 12)));
			jPanel6.add(saveInsertedCommandIntoConsoleIntoFile(),
					new Constraints(new Leading(834, 10, 10), new Leading(189,
							12, 12)));
			jPanel6.add(ListWithCommandsFromFile(), new Constraints(
					new Leading(408, 450, 10, 10), new Leading(6, 135, 10, 10)));
			jPanel6.add(runCommandSelectedFromList(), new Constraints(
					new Leading(887, 10, 10), new Leading(57, 10, 10)));
			jPanel6.add(openEditableFormForCommands(), new Constraints(
					new Leading(887, 10, 10), new Leading(57+40, 10, 10)));
		}
		return jPanel6;
	}

	private JPanel createPanel(String name)
			throws exc.twoDefaultValuesCanNotExists {
		JPanel tempa = new JPanel();
		tempa.setLayout(new GroupLayout());
		tempa = generatePanel(tempa, name, name);
		return tempa;
	}

	/**
	 * update
	 * 
	 * @param tab
	 * @param newcommand
	 */
	void UpdateCommand(String tab, String newcommand) {
		if (actualCommand.containsKey(tab)) {
			actualCommand.put(tab, newcommand);
		} else
			actualCommand.put(tab, newcommand);
	}

	static Map<String, String> zakladki = new HashMap<String, String>();
	{
		zakladki.put("U", "Upload");
		zakladki.put("P", "Provision");
	}

	/**
	 * actual commandwhich was generated
	 * 
	 * @param whichTab
	 * @return
	 */
	String getActualCommand(String whichTab) {
		return actualCommand.get(whichTab);
	}

	/**
	 * <b>rncSomename*</b>-><b>rncSomename</b><br/>
	 * <b>rncSomename-</b>-><b>rncSomename</b><br/>
	 * <b>rncSomename#</b>-><b>rncSomename</b><br/>
	 * 
	 * @param actuallabel
	 * @return
	 */
	String clearlabel(String actuallabel) {
		String fixedlabel = "";
		boolean isFix = false;
		String[] possiblecharactersnotallowed = { "#", "-", "*" };
		for (String value : possiblecharactersnotallowed) {
			if (actuallabel.indexOf(value) != -1) {
				isFix = true;
				fixedlabel = actuallabel.replace(value, "");
			}
		}
		return isFix ? fixedlabel : actuallabel;
	}

	private void rem()
	{
		
	}
	/**
	 * whichOneParameterIsNeeded
	 * FIRST-0
	 * LAST -1
	 */
	private String firstParameterAndLast(Map<String, String> x,String whichOneParameterIsNeeded)
	{
		String hh="";
		if( "FIRST".equals(whichOneParameterIsNeeded) && x.containsKey(whichOneParameterIsNeeded))
		{
			hh=" -"+x.get(whichOneParameterIsNeeded);
		}
		if( "LAST".equals(whichOneParameterIsNeeded) && x.containsKey(whichOneParameterIsNeeded))
		{
			hh=" -"+x.get("LAST");
		}
		if  ( hh=="" && "FIRST".equals(whichOneParameterIsNeeded))
			hh="";
		else if  ( hh=="" && "LAST".equals(whichOneParameterIsNeeded))
			hh=lastParameter_V;
		
		
		return hh;
	}
	private JPanel generatePanel(JPanel tyymp, String operation_, String h)
			throws exc.twoDefaultValuesCanNotExists {
		int x = 9, y = 11, li = 0;
		/**
		 * set standard command to each of the function racclimx.sh -op Upload
		 */
		Map<String, String> 
			temp = new HashMap<String, String>(), 
			temp2 = new HashMap<String, String>();
		
		// here get only first char ( e.g. P <- Provision,U <-Upload =first letter of filename
		tempPrefix = h.substring(0, 1) + "-";
		Map<String, String> checkbox1 = TESTXML.czytajPlikXmlZParametrami(h);
		if ( checkbox1.containsKey("COMMAND") )
		{
			mapWithStandardCommands.put(h, checkbox1.get("COMMAND")+firstParameterAndLast(checkbox1,"FIRST"));
			checkbox1.remove("FIRST");
			last.put(checkbox1.get("COMMAND"), firstParameterAndLast(checkbox1,"LAST"));
			checkbox1.remove("LAST");
			checkbox1.remove("COMMAND");
		}
		else
			mapWithStandardCommands.put(h, RACCLIMX + h);
		
		for (String a : checkbox1.keySet()) {
			if (a.length() == 3) {
				if (a.indexOf("*") != -1) {
					tyymp.add(etykieta(a), new Constraints(new Leading(x, 200,
							30 + 5, 30 + 5), new Leading(y, 30 + 5, 30 + 5)));
					tyymp.add(poletxt(checkbox1, a, temp, temp2),
							new Constraints(new Leading((x + 100), 200, 30 + 5,
									30 + 5), new Leading(y, 30 + 5, 30 + 5)));
				}
				y += 20;
				++li;
				if (li == 10) {
					x += 300;
					y = 11;
					li = 0;
				}
			}
			if (a.length() >= 4) {
				if (!a.substring(0, 4).equals("core")) {
					if (a.indexOf("*") != -1) {
						tyymp.add(etykieta(a), new Constraints(new Leading(x,
								200, 30 + 5, 30 + 5), new Leading(y, 30 + 5,
								30 + 5)));
						tyymp.add(poletxt(checkbox1, a, temp, temp2),
								new Constraints(new Leading((x + 100), 200,
										30 + 5, 30 + 5), new Leading(y, 30 + 5,
										30 + 5)));
					} else if (a.indexOf("#") != -1) {
						tyymp.add(etykieta(a), new Constraints(new Leading(x,
								110, 8, 8), new Leading(y, 8, 8)));
						tyymp.add(combo(checkbox1, a), new Constraints(
								new Leading((x + 170), 110, 8, 8), new Leading(
										y, 8, 8)));
					} else {
						tyymp.add(czekbox(checkbox1, a), new Constraints(
								new Leading(x, 8, 8), new Leading(y, 8, 8)));
					}
					y += 20;
					++li;
					if (li == 10) {
						x += 300;
						y = 11;
						li = 0;
					}
				}
			}
		}
		tyymp.add(executeButton(), new Constraints(
				new Leading(1315, 110, 8, 8), new Leading(200, 8, 8)));
		
		//button to reset panel
		tyymp.add(resetPanelButton(), new Constraints(
				new Leading(1315, 110, 8, 8), new Leading(200-40, 8, 8)));
		
		checkbox1.clear();
		return tyymp;
	}

	JButton executeButton() {
		JButton execute = new JButton("Execute");
		execute.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				addFlag = true;
				runCommandFromLabel(arg0);
				
				addFlag = false;
			}
		});
		return execute;
	}
	JButton resetPanelButton() {
		JButton execute = new JButton("Reset");
		execute.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				addFlag = true;
				InternalFrameDemo.ff();//updateList();
				resetpanel( actualselectedTab );
				/*Thread tt=new Thread(new Runnable() {
					
					@Override
					public void run() {
						LabWindow.koniecpolecenia=false;
						while(LabWindow.koniecpolecenia){
							try {
								Thread.sleep(200);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
							System.out.println("czekam...................");
						}
						String t="Provision";
						Map<String, String> temp = InternalFrameDemo.getMap("P");
						System.out.println("MAPA>"+temp);
						Component[] components1 = getContentPane().getComponents();
						Component component = null;
						for (int i = 0; i < components1.length; i++) {
							component = components1[i];
							if (component instanceof JTabbedPane) {
								int count = ((JTabbedPane) component).getTabCount();
								for (int i1 = 0; i1 < count; i1++) {
									if( ((JTabbedPane) component).getTitleAt(0)=="Provision")
										((JTabbedPane) component).setEnabled(false);
									Component com = ((JTabbedPane) component)
											.getComponentAt(i1);
									if (com instanceof JPanel) {
										System.out.println("NAZWAPANELU:"+ ((JPanel) com).getName());
										Component[] components = ((JPanel) com).getComponents();
										for (int a = 0; a < components.length; a++) {
											System.out.println("C"+components[a]);
											if( components[a].getName()!=null )
											{
												String ta=t.substring(0,1)+"-";
												if( components[a].getName().indexOf(ta)!=-1)
												{
													changeComponent1(components[a], ";"+temp.get(components[a].getName()).toString(),true);
													actual.remove(components[a].getName());
													actual.put(components[a].getName(),temp.get(components[a].getName()));
												}
											}
										}
									
									}
								}
							}
						}
					}
				});
				tt.start();*/
				updateActualCommandAndPutItIntoLabel(mapWithStandardCommands.get(actualselectedTab));
				//reset
				addFlag = false;
			}
		});
		return execute;
	}
	JTextField poletxt(Map<String, String> labelsnameWithDefaultValues,
			String labeltxtwithstrangecharacters, Map<String, String> aa,
			Map<String, String> ab) {
		JTextField txtfield = new JTextField();
		txtfield.setName(tempPrefix + clearlabel(labeltxtwithstrangecharacters));
		/**
		 * save value of parameter to static map
		 */
		InternalFrameDemo
				.addValueToCommonMap(
						txtfield.getName(),
						(labelsnameWithDefaultValues
								.get(labeltxtwithstrangecharacters) == "empty") ? ""
								: labelsnameWithDefaultValues
										.get(labeltxtwithstrangecharacters),
						tempPrefix.substring(0, 1));
		/**
		 * add parameter and his value to actual map during generate panel
		 */
		actual.put(txtfield.getName(),
				labelsnameWithDefaultValues.get(labeltxtwithstrangecharacters));
		txtfield.setForeground(Color.BLUE);
		txtfield.addKeyListener(new KeyListener() {
			@Override
			public void keyTyped(KeyEvent arg0) {
				StringBuffer val = new StringBuffer(((JTextField) arg0
						.getSource()).getText());
				String componentName=((JTextField) arg0.getSource()).getName();
				if (arg0.getKeyChar() == 8) 
				{
					actual.remove(componentName);
					if (val.length() >= 1) 
					{
						actual.put(componentName,
								val.toString());
					} 
					else if (val.length() == 0) 
					{
						actual.put(componentName,"");
					}
				} 
				else 
				{
					val.append(arg0.getKeyChar());
					actual.put(((JTextField) arg0.getSource()).getName(),
							val.toString());
				}
				String completeCommand = InternalFrameDemo
						.mergeStandardAndActualValues(actual,
								actualselectedTab.substring(0, 1));
				updateActualCommandAndPutItIntoLabel(mapWithStandardCommands
						.get(actualselectedTab)
						+ " "
						+ completeCommand
						+ " "
						+ returnLastParameter());
			}

			@Override
			public void keyReleased(KeyEvent arg0) {
			}

			@Override
			public void keyPressed(KeyEvent arg0) {
			}
		});
		return txtfield;
	}

	JComboBox combo(Map<String, String> labelsnameWithDefaultValues,
			String labeltxtwithstrangecharacters)
			throws exc.twoDefaultValuesCanNotExists {
		JComboBox component = new JComboBox();
		DefaultComboBoxModel dmodel = new DefaultComboBoxModel();
		component.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String newSelection = (String) ((JComboBox) arg0.getSource())
						.getSelectedItem();
				newSelection = newSelection.replace("-", "");
				actual.remove(((JComboBox) arg0.getSource()).getName());
				actual.put(((JComboBox) arg0.getSource()).getName(), String
						.valueOf(((JComboBox) arg0.getSource())
								.getSelectedItem()));
				updateActualCommandAndPutItIntoLabel(mapWithStandardCommands
						.get(actualselectedTab)
						+ " "
						+ generatePartOfCommand(actual,
								actualselectedTab.substring(0, 1))
						+ " "
						+ returnLastParameter());
			}
		});
		String[] element = labelsnameWithDefaultValues.get(
				labeltxtwithstrangecharacters).split("/");
		boolean twoDefaultValuesCanNotExists = false;
		for (String elements : element) {
			if (elements.indexOf("-") != -1) {
				if (twoDefaultValuesCanNotExists)
					throw new exc.twoDefaultValuesCanNotExists(
							"Two default values in xml file can not exists. Only one."
									+ "See xml file.");

				// add parameter with values
				InternalFrameDemo.addValueToCommonMap(
						clearlabel(labeltxtwithstrangecharacters),
						clearlabel(elements), tempPrefix.substring(0, 1));
				twoDefaultValuesCanNotExists = true;
				actual.put(clearlabel(labeltxtwithstrangecharacters),
						clearlabel(elements));
			}
			dmodel.addElement(clearlabel(elements));
		}
		component.setModel(dmodel);
		if (labeltxtwithstrangecharacters.indexOf("-") != -1)
			component.setFont(new Font("Monospaced", Font.PLAIN, 12));
		component.setName(tempPrefix
				+ clearlabel(labeltxtwithstrangecharacters));
		/**
		 * add parameter and his value to actual map during generate panel
		 */
		actual.put(component.getName(),
				labelsnameWithDefaultValues.get(labeltxtwithstrangecharacters));
		/**
		 * save value of parameter to static map
		 */
		InternalFrameDemo.addValueToCommonMap(component.getName(),
				labelsnameWithDefaultValues.get(labeltxtwithstrangecharacters),
				tempPrefix.substring(0, 1));
		return component;
	}

	String generatePartOfCommand(Map<String, String> ac, String tab) {
		return InternalFrameDemo.mergeStandardAndActualValues(ac,
				tab.substring(0, 1));
	}
	String returnLastParameter()
	{
		if ( last.containsKey(mapWithStandardCommands
				.get(actualselectedTab)))
		return
				last.get(mapWithStandardCommands
						.get(actualselectedTab));
						else
							return lastParameter_V;
	}

	JCheckBox czekbox(Map<String, String> labelsnameWithDefaultValues,
			String labeltxtwithstrangecharacters) {
		JCheckBox component = new JCheckBox();
		component.setToolTipText("test");
		component.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String componentName=((JCheckBox) arg0.getSource()).getName();
				actual.remove(componentName);
				actual.put(componentName, String
						.valueOf(((JCheckBox) arg0.getSource()).isSelected()));					//P|U-costam,true|false
				updateActualCommandAndPutItIntoLabel(mapWithStandardCommands
						.get(actualselectedTab)
						+ " "
						+ generatePartOfCommand(actual,
								actualselectedTab.substring(0, 1))
						+ " "
						+ returnLastParameter());
				//if standard!=actual -> change color
				if (
					InternalFrameDemo.getStandardPropertyForParamater(
							actualselectedTab.substring(0, 1),componentName) 
							.equals(
					actual.get(componentName).toString()))
					((JCheckBox) arg0.getSource()).setForeground(new Color(51,51,51));			// jesli true==true -> standard==actual
				else
					((JCheckBox) arg0.getSource()).setForeground(Color.BLUE);					// jesli true!=true -> standard!=actual
			}
		});
		component.setSelected(labelsnameWithDefaultValues.get(
				labeltxtwithstrangecharacters).equals("true") ? true : false);
		if (labeltxtwithstrangecharacters.indexOf("-") != -1)
			component.setFont(new Font("Monospaced", Font.PLAIN, 12));
		component.setText(clearlabel(labeltxtwithstrangecharacters));
		component.setName(tempPrefix
				+ clearlabel(labeltxtwithstrangecharacters));
		/**
		 * add parameter and his value to actual map during generate panel
		 */
		actual.put(component.getName(),
				labelsnameWithDefaultValues.get(labeltxtwithstrangecharacters));
		/**
		 * save value of parameter to static map
		 */
		InternalFrameDemo.addValueToCommonMap(component.getName(),
				labelsnameWithDefaultValues.get(labeltxtwithstrangecharacters),
				tempPrefix.substring(0, 1));
		/**
		 * map with actual values
		 */
		return component;
	}

	void updateActualCommandAndPutItIntoLabel(String command) {
		actualCommand.put(zakladki.get(actualselectedTab.substring(0, 1)), command);
		//UpdateCommand(zakladki.get(actualselectedTab.substring(0, 1)), command);					
		putcommandintogui(actualCommand.get(zakladki.get(actualselectedTab
				.substring(0, 1))));
	}
	void clearMap(String key)
	{
		Map<String,String> tymp=InternalFrameDemo.getMap(key);
		
		for(String key_:tymp.keySet())
		{
			actual.remove(key_);
			actual.put(key, tymp.get(key_));
		}
	}

	/**
	 * generuje etykiete TYLKO
	 * 
	 * @param value
	 *            -- nazwa widoczna na etykiecie
	 * @return JLabel
	 */
	JLabel etykieta(String value) {
		JLabel component = new JLabel(clearlabel(value) + " :");
		if (value.indexOf("-") != -1)
			component.setFont(new Font("Monospaced", Font.PLAIN, 12));
		return component;
	}

	void updateComboBoxWithExecutedCommands(String command) {
		boolean c = listapolecenwykonanych.add(command);
		if (c) {
			Iterator p = listapolecenwykonanych.iterator();
			if (listapolecen.getSize() != 0)
				listapolecen.removeAllElements();
			while (p.hasNext())
				listapolecen.addElement(p.next());
		}

	}

	/**
	 * put command only into label on the panel - under TextArea
	 * 
	 * @param insertedCommand
	 */
	void putcommandintogui(String insertedCommand) {
		jLabel1.setText(insertedCommand);
	}

	/**
	 * execute commmand in the console
	 * 
	 * @param command
	 */
	void executeCommandInConsole(String command) {
		updateComboBoxWithExecutedCommands(command);
		try {
			ct.td.out
					.write((command + "\n").getBytes(Charset.forName("UTF-8")));
		} catch (Exception e) {
		}
	}

	private boolean addFlag = false;

	private JComboBox createComboBoxWithCommandsHistory() {
		if (jComboBox1 == null) {
			jComboBox1 = new JComboBox();
			listapolecen = new DefaultComboBoxModel();
			jComboBox1.setModel(listapolecen);
			jComboBox1.setDoubleBuffered(false);
			jComboBox1.setBorder(null);
			jComboBox1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					if (!addFlag) {
						new ParseCommand((String) ((JComboBox) event
								.getSource()).getSelectedItem());
						selectedCommandFromComboBox(event);
					}
				}
			});
		}
		return jComboBox1;
	}

	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setEnabled(false);
			jTextField1.addKeyListener(new KeyListener() {
				@Override
				public void keyTyped(KeyEvent e) {
					System.out.println("typed");
				}

				@Override
				public void keyReleased(KeyEvent e) {
					jButton2.setEnabled((jTextField1.getText().length() == 0) ? false
							: true);
				}

				@Override
				public void keyPressed(KeyEvent e) {

				}
			});
		}
		return jTextField1;
	}

	private JLabel getJLabel2() {
		if (jLabel2 == null) {
			jLabel2 = new JLabel();
			jLabel2.setText("Save last inserted command at command line as:");
		}
		return jLabel2;
	}

	/**
	 * list with commands from file
	 * 
	 * @return
	 */
	private JList getJList0() {
		if (jList0 == null) {
			jList0 = new JList();
			listModel = new DefaultListModel();
			Map<String, String> c = new File_().readF(0);
			listModel.clear();
			if (!c.isEmpty()) {
				for (String b : c.keySet()) {
					listModel.addElement(b);
				}
			}
			jList0.setModel(listModel);
			jList0.addMouseListener(new MouseAdapter() {

				public void mouseClicked(MouseEvent event) {
					executeCommandFromList(event);
				}
			});
		}
		return jList0;
	}

	private JTextArea getJTextArea0() {
		if (jTextArea0 == null) {
			jTextArea0 = new JTextArea();
			jTextArea0.setWrapStyleWord(true);
			jTextArea0.setLineWrap(true);
		}
		return jTextArea0;
	}

	private JTextField getJLabel1() {
		if (jLabel1 == null) {
			jLabel1 = new JTextField("");
			jLabel1.setEditable(false);
		}
		return jLabel1;
	}

	/**
	 * generate tabs
	 * 
	 * @return
	 */
	private JTabbedPane getJTabbedPane0()
			throws exc.twoDefaultValuesCanNotExists {
		String firstVisibleTab = "";
		if (jTabbedPane0 == null) {
			jTabbedPane0 = new JTabbedPane();
			
			PLIKI zz=new PLIKI();
			Set<String> tabs = zz.get()/*new TreeSet<String>()*/;
			/*tabs.add("Upload");
			tabs.add("Provision");*/
			for (String it : tabs) {
				if (firstVisibleTab == "")
					firstVisibleTab = it;
				if ( !zakladki.containsKey(it.substring(0,1)))
					zakladki.put(it.substring(0,1), it);
				jTabbedPane0.addTab(
						it/*"Provision"*/,
						createPanel(it/*"Provision"*/));
			}
			jTabbedPane0.addTab("My Commands", generatePanelMYCOMMANDS());
			jTabbedPane0.addChangeListener(new ChangeListener() {
				public void stateChanged(ChangeEvent event) {
					actualselectedTab = jTabbedPane0.getTitleAt(jTabbedPane0
							.getSelectedIndex());
					if (actualCommand.containsKey(jTabbedPane0
							.getTitleAt(jTabbedPane0.getSelectedIndex()))) {
						putcommandintogui(actualCommand.get(jTabbedPane0
								.getTitleAt(jTabbedPane0.getSelectedIndex())));
					} else {
						putcommandintogui(mapWithStandardCommands
								.get(actualselectedTab));
					}
				}
			});
			/**
			 * set first tab
			 */
			actualselectedTab = firstVisibleTab;
		}
		return jTabbedPane0;
	}

	/**
	 * the main TextArea
	 * 
	 * @return
	 */
	private JScrollPane getJScrollPane1() {
		if (jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.setViewportView(getJTextArea0());
			jScrollPane1
					.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

		}
		return jScrollPane1;
	}

	private JScrollPane getJScrollPane0() {
		if (jScrollPane0 == null) {
			jScrollPane0 = new JScrollPane();
			jScrollPane0.setViewportView(getJList0());
		}
		return jScrollPane0;
	}

	/**
	 * execute command from list
	 * 
	 * @param event
	 */
	private void executeCommandFromList(MouseEvent event) {
		if (((JList) event.getSource()).getSelectedValue() != null) {
			if (event.getSource() instanceof JList) {
				for (String key : File_.common_map_commands.keySet()) {
					if (key.equals(((JList) event.getSource())
							.getSelectedValue().toString())) {
						putcommandintogui(File_.common_map_commands.get(key));
						changeVisibilityButtonResponsibleToExecuteCommandfroomList(true);
					}
				}
			}
		} else
			JOptionPane.showMessageDialog(this,
					"List is empty.Please select from menu Labs - > Commands.");
	}

	/**
	 * command selected from combobox
	 * 
	 * @param event
	 */
	private void selectedCommandFromComboBox(ActionEvent event) {
		String commandFromComboBox = (String) ((JComboBox) event.getSource())
				.getSelectedItem();
		parsecommand(this, commandFromComboBox);
		//resetpanel( tab(commandFromComboBox) );
		putcommandintogui(commandFromComboBox);
		executeCommandInConsole(commandFromComboBox);
	}
	private String tab(String a)
	{
		String b=null;
		if ( a.indexOf(" Provision ")!=-1)
			b="P";
		if ( a.indexOf(" Upload ")!=-1)
			b="U";
		return b;
		
	}
	private void blockAllTabsForExecutingCommandTime()
	{
		
	}
	Map<String, String> parsecommandtoMap(String h) {
		String w = null;
		Map<String, String> temp = new HashMap<String, String>();
		String[] parametry = h.split(" ");
		w = parametry[2].substring(0, 1);
		;
		for (int a = 3; a < parametry.length; a += 2) {
			if (parametry[a].indexOf("-") == -1)
				break;
			if (parametry[a].indexOf("-") != -1)
				temp.put(w + parametry[a], parametry[a + 1]);
		}
		return temp;
	}
	/**
	 * update list with command when command was deleted or it has other name
	 */
	public void updateList()
	{
		InternalFrameDemo.ff();//updateList();
	}
	private void parsecommand(final Component c, String h) {
		Map<String, String> temp = parsecommandtoMap(h);
		Component[] components1 = getContentPane().getComponents();
		Component component = null;
		for (int i = 0; i < components1.length; i++) {
			component = components1[i];
			if (component instanceof JTabbedPane) {
				int count = ((JTabbedPane) component).getTabCount();
				for (int i1 = 0; i1 < count; i1++) {
					Component com = ((JTabbedPane) component)
							.getComponentAt(i1);
					if (com instanceof JTabbedPane) {
						System.out.println("PANEL");
					}
					if (com instanceof JPanel) {
						Component[] components = ((JPanel) com).getComponents();
						// get all components from PANEL
						for (int a = 0; a < components.length; a++) {
							if (temp.containsKey(components[a].getName())) {
								changeComponent(components[a],
										temp.get(components[a].getName()));
							}
							else
								components[a].setForeground(new Color(51,51,51));
						}
					}
				}
			}
		}
	}
	void ENABLE(){
		String t=actualselectedTab;//"Provision";
			//get all standard settings from map and set all components in panel as in map
			Map<String, String> temp = InternalFrameDemo.getMap(t.substring(0,1)/*"P"*/);
			//tutaj wszystkie komponenty glownego panelu jakie sa
			Component[] components1 = getContentPane().getComponents();
			Component component = null;
			for (int i = 0; i < components1.length; i++) {
				component = components1[i];
				if (component instanceof JTabbedPane) {
					//pobierz wszystkie taby
					int count = ((JTabbedPane) component).getTabCount();
					for (int i1 = 0; i1 < count; i1++) {
						//NAZWA TAB -PROVISION etc
						if( ((JTabbedPane) component).getTitleAt(0)==actualselectedTab/*"Provision"*/)
							((JTabbedPane) component).setEnabled(false);
						/*((JTabbedPane) component).setVisible(false);*/
						/*.setEnabled(false);*/
						Component com = ((JTabbedPane) component)
								.getComponentAt(i1);
						if (com instanceof JPanel) {
							System.out.println("NAZWAPANELU:"+ ((JPanel) com).getName());
							Component[] components = ((JPanel) com).getComponents();
							// get all components and set them
							doSomethind(components,temp,t,"+");
						}
						//}
					}
				}
			}
	}
	void DisableEnable(String char_)
	{
		String t=actualselectedTab/*"Provision"*/;
		//get all standard settings from map and set all components in panel as in map
		Map<String, String> temp = InternalFrameDemo.getMap(t.substring(0,1)/*"P"*/);
		//tutaj wszystkie komponenty glownego panelu jakie sa
		Component[] components1 = getContentPane().getComponents();
		Component component = null;
		for (int i = 0; i < components1.length; i++) {
			component = components1[i];
			if (component instanceof JTabbedPane) {
				//pobierz wszystkie taby
				int count = ((JTabbedPane) component).getTabCount();
				for (int i1 = 0; i1 < count; i1++) {
					//NAZWA TAB -PROVISION etc
					if( ((JTabbedPane) component).getTitleAt(0)==actualselectedTab/*"Provision"*/)
						((JTabbedPane) component).setEnabled(false);
					Component com = ((JTabbedPane) component)
							.getComponentAt(i1);
					if (com instanceof JPanel) {
						System.out.println("NAZWAPANELU:"+ ((JPanel) com).getName());
						Component[] components = ((JPanel) com).getComponents();
						doSomethind(components,temp,t,char_);
					}
				}
			}
		}
	}
	void disaBBle(){
		String t=actualselectedTab/*"Provision"*/;
			//get all standard settings from map and set all components in panel as in map
			Map<String, String> temp = InternalFrameDemo.getMap(t.substring(0,1));
			//tutaj wszystkie komponenty glownego panelu jakie sa
			Component[] components1 = getContentPane().getComponents();
			Component component = null;
			for (int i = 0; i < components1.length; i++) {
				component = components1[i];
				if (component instanceof JTabbedPane) {
					//pobierz wszystkie taby
					int count = ((JTabbedPane) component).getTabCount();
					for (int i1 = 0; i1 < count; i1++) {
						//NAZWA TAB -PROVISION etc
						if( ((JTabbedPane) component).getTitleAt(0)==actualselectedTab)
							((JTabbedPane) component).setEnabled(false);
						Component com = ((JTabbedPane) component)
								.getComponentAt(i1);
						if (com instanceof JPanel) {
							System.out.println("NAZWAPANELU:"+ ((JPanel) com).getName());
							Component[] components = ((JPanel) com).getComponents();
							doSomethind(components,temp,t,"-");
						}
					}
				}
			}
	}
	void doSomethind(Component[] components,Map<String, String> temp,String t,String char_)
	{
		for (int a = 0; a < components.length; a++) 
		{
			System.out.println("C"+components[a]);
			if( components[a].getName()!=null )
			{
				String ta=t.substring(0,1)+"-";
				if( components[a].getName().indexOf(ta)!=-1)
				{
					changeComponent1(components[a], ";"+temp.get(components[a].getName()).toString(),(char_=="-")?false:true);
					//set actual 
					actual.remove(components[a].getName());
					actual.put(components[a].getName(),temp.get(components[a].getName()));
				}
			}
		}
	}
	void resetpanel(String t)
	{
		//get all standard settings from map and set all components in panel as in map
		Map<String, String> temp = InternalFrameDemo.getMap(t.substring(0,1));
		//tutaj wszystkie komponenty glownego panelu jakie sa
		Component[] components1 = getContentPane().getComponents();
		Component component = null;
		for (int i = 0; i < components1.length; i++) {
			component = components1[i];
			if (component instanceof JTabbedPane) {
				//pobierz wszystkie taby
				int count = ((JTabbedPane) component).getTabCount();
				for (int i1 = 0; i1 < count; i1++) {
					//NAZWA TAB -PROVISION etc
					/*if( ((JTabbedPane) component).getTitleAt(0)==actualselectedTab)
						((JTabbedPane) component).setEnabled(false);*/
					Component com = ((JTabbedPane) component)
							.getComponentAt(i1);
					if (com instanceof JPanel) {
						System.out.println("NAZWAPANELU:"+ ((JPanel) com).getName());
						Component[] components = ((JPanel) com).getComponents();
						doSomethind(components,temp,t,"+");
					}
				}
			}
		}
	}
	/**
	 *  if value has a <b>;</b> char set Color of Component to standard 
	 * ---------------------------------------------------------
	 * set all components as selected command from combobox<br/>
	 * or reset component to his first state -as read from xml file (value parameter has ";" char at the begining of value -> ";P-rncOmc..."
	 * @param com
	 * @param value
	 */
	void changeComponent(Component com, String value) {
		if (com instanceof JCheckBox) {
			((JCheckBox) com).setSelected( value.equals("false") || value.equals(";false") ? false : true);
			((JCheckBox) com).setForeground((value.indexOf(";")!=-1)?new Color(51,51,51):Color.BLUE);
		}
		if (com instanceof JTextField) {
			((JTextField) com).setText((value.indexOf(";")!=-1)?"":value);
			((JTextField) com).setForeground((value.indexOf(";")!=-1)?new Color(51,51,51):Color.BLUE);
		}
		if (com instanceof JComboBox) {
			((JComboBox) com).setSelectedItem((value.indexOf(";")!=-1)?value.substring(1):value);
		}
	}
	/**
	 * if <b>a</b> has <b>true|false</b> then set component to enable or disable.
	 * the rest of parameters are the same as changeComponent method 
	 * @param com
	 * @param value
	 * @param a
	 */
	void changeComponent1(Component com, String value,boolean a) {
		if (com instanceof JCheckBox) {
			((JCheckBox) com).setSelected( value.equals("false") || value.equals(";false") ? false : true);
			((JCheckBox) com).setForeground((value.indexOf(";")!=-1)?new Color(51,51,51):Color.BLUE);
			((JCheckBox) com).setEnabled(a);
		}
		if (com instanceof JTextField) {
			((JTextField) com).setText((value.indexOf(";")!=-1)?"":value);
			((JTextField) com).setForeground((value.indexOf(";")!=-1)?new Color(51,51,51):Color.BLUE);
			((JTextField) com).setEnabled(a);
		}
		if (com instanceof JComboBox) {
			((JComboBox) com).setSelectedItem((value.indexOf(";")!=-1)?value.substring(1):value);
		}
	}
	/**
	 * action to execute command selected from list
	 * 
	 * @param event
	 */
	private void runCommandFromLabel(ActionEvent event) {
		if (jLabel1.getText().toString() != null
				|| jLabel1.getText().toString() != "")
			executeCommandInConsole(jLabel1.getText());
		else
			JOptionPane.showMessageDialog(this,
					"Please select command from list.");
	}

	/*
	 * STATIC
	 */
	static String 
		commandPutFromTextArea = "";
	
	static boolean polaczenie_z_ip;
	/**
	 * standard commands<br/>
	 * racclimx.sh -op Upload
	 */
	private static Map<String, String> 
		mapWithStandardCommands = new HashMap<String, String>(),
		last = new HashMap<String, String>();
	
	private Map<String, String> 
		actualCommand = new HashMap<String, String>(),
		actual = new HashMap<String, String>();
	/**
	 * PRIVATE STATIC FINAL
	 */
	private static final String RACCLIMX = "racclimx.sh -op ";

	/**
	 * DEFAULT
	 */
	String 
		directory = "",host, username,shortName;
	private String 
			tempPrefix = "", 
			actualselectedTab = "", 
			lastParameter_V = " -v", 
			newCommandName = "";
	/**
	 * list with all commands which were executing on the lab
	 */
	private Set<String> listapolecenwykonanych = new HashSet<String>();

	_ConnectionThread ct, watek;
	/*
	 * private Session ses; private InputStream in; private OutputStream out;
	 */
	/**
	 * list with all commands from file
	 */
	private DefaultListModel listModel;
	/**
	 * model - list which were executing on the lab
	 */
	private DefaultComboBoxModel listapolecen = null;
	/**
	 * actual tab on the view </br> <b>Upload</b></br> <b>Provision</b></br>
	 * .....
	 */
	/**
	 * PRIVATE
	 */
	private JPanel jPanel6;
	private JList jList0, jList2;
	private JButton jButton2;
	private static JButton jButton3,jButton4;
	private JScrollPane jScrollPane3;
	private JTextArea jTextArea0;
	private JTextField jTextField1, jLabel1;
	private JLabel jLabel2/* , jLabel1 */;
	private JComboBox jComboBox1;
	private static final long serialVersionUID = 1L;
	private JScrollPane jScrollPane0, jScrollPane1;
	private JTabbedPane jTabbedPane0;

}
